package com.heima.behavior.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.dto.UnlikesBehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior> implements IApUnlikesBehaviorService {

    @Autowired
    private IApBehaviorEntryService entryService;

    @Override
    public ResponseResult<Integer> unlikesBehavior(UnlikesBehaviorDto dto) {
        EntryDto entryDto = new EntryDto();
        entryDto.setUserId(AppThreadLocalUtil.get().getUserId());
        entryDto.setEquipmentId(dto.getEquipmentId());
        ApBehaviorEntry entry = entryService.getEntry(entryDto);
        dto.setEntryId(Integer.valueOf(entry.getEntryId()));
        //查询该用户或设备有无对该文章的操作
        LambdaQueryWrapper<ApUnlikesBehavior> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUnlikesBehavior::getArticleId,dto.getArticleId()).eq(ApUnlikesBehavior::getEntryId,dto.getEntryId());
        ApUnlikesBehavior apUnlikesBehavior = getOne(queryWrapper);
        if(apUnlikesBehavior==null) {
            apUnlikesBehavior = new ApUnlikesBehavior();
        }
        BeanUtils.copyProperties(dto,apUnlikesBehavior);
        apUnlikesBehavior.setType(dto.getType());
        if (apUnlikesBehavior.getId()==null) {
            apUnlikesBehavior.setCreatedTime(new Date());
        }
       saveOrUpdate(apUnlikesBehavior);

        return ResponseResult.okResult(apUnlikesBehavior.getType());
    }


}
